<template>
  <div class="app-viewer">
    <div class="viewer-header">
      <el-row>
        <el-col :span="6">
          <el-row :gutter="10" class="mb20">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="18" style="text-align: right">
          <transition name="fade">
            <span v-show="!isShowQuery">
              <el-input v-model.number="queryParams.sorId" placeholder="请输入传感器ID" style="width: 200px; margin-right: 10px"
                oninput="value=value.replace(/[^\d]/g,'')"></el-input>
              <el-input v-model="queryParams.address" placeholder="请输入所属火区地址"
                style="width: 200px; margin-right: 10px"></el-input>
              <el-date-picker v-model="dateRange" style="width: 250px; margin-right: 10px" type="datetimerange"
                size="small" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                value-format="yyyy-MM-dd hh:mm:ss" @change="dateHandler" popper-class="lcj-select-popup"></el-date-picker>
              <el-button type="primary" @click="handleQuery">查询</el-button>
              <el-button type="primary" @click="resetQuery">重置</el-button>
            </span>
          </transition>
          <!-- <el-button type="primary" @click="switchQuery">
            {{ isShowQuery ? "普通检索" : "高级检索" }}
          </el-button>-->
        </el-col>
      </el-row>
      <!-- <el-collapse-transition>
        <div class="query" v-show="isShowQuery">
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="100px"
            @submit.native.prevent
          >
            <el-form-item prop="sorId">
              <el-input
                v-model="queryParams.sorId"
                placeholder="请输入传感器id"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="address">
              <el-input
                v-model="queryParams.address"
                placeholder="请输入所属火区地址"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="" prop="time">
              <el-date-picker
                v-model="dateRange"
                 size="mini"
                type="datetimerange"
                size="small"
                range-separator="至"
                
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd hh:mm:ss"
                @change="dateHandler"
                popper-class="lcj-select-popup"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button type="primary" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-transition>-->
    </div>

    <div class="viewer-body">
      <el-table :data="infoList" border :header-cell-style="{
        background: '#Fff',
        fontFamily: 'AlibabaPuHuiTi-2-55-Regular',
      }">
        <el-table-column label="传感器ID" width="100" align="center" prop="sorId" />
        <el-table-column label="所属火区" align="center" prop="address" show-overflow-tooltip />
        <el-table-column prop="lonAndLat" align="center" label="经纬度"></el-table-column>
        <el-table-column prop="qaType" align="center" label="问题类型">
          <template slot-scope>故障</template>
        </el-table-column>
        <el-table-column label="开始时间" align="center" prop="starttime"></el-table-column>
        <el-table-column label="关闭时间" align="center" prop="endtime"></el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <div @click="handleDetail(scope.row)" class="checkDetail">查看</div>
            <!-- <div @click="handleExport(scope.row)" class="checkDetail">导出</div> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" layout="total, prev,pager,next" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getPageData" />
    </div>

    <el-dialog :title="title" :visible.sync="open" class="open-container" @opened="initChart" @closed="closeDialog"
      :modal-append-to-body="false">
      <el-date-picker type="datetimerange" size="mini" v-model="dateDialogRange" range-separator="至"
        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd hh:mm:ss" @change="dateDialogHandler"
        popper-class="lcj-select-popup"></el-date-picker>
      <div id="myChart" class="myChart"></div>
    </el-dialog>
  </div>
</template>

<script>
import { faultList } from "@/api/data/houtai";
import { formatDate } from "@/utils/index";
import { getTempAndSensorvoltage } from "@/api/data/houtai";
let intervalKey;
export default {
  name: "Info",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史数据表格数据
      allList: [],
      // 历史数据表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sorId: "",
        address: "",
        startTime: formatDate(
          new Date().setFullYear(new Date().getFullYear() - 1)
        ),
        endTime: formatDate(new Date())
      },
      isShowQuery: false,

      // 日期范围
      dateRange: [
        formatDate(new Date().setFullYear(new Date().getFullYear() - 1)),
        formatDate(new Date())
      ],
      myChart: null,
      //温度y值
      sensortempList: [],
      //气压y值
      sensorvoltageList: [],

      open: false,
      curSenorID: "",
      dateDialogRange: [],
      // 用于接收路由跳转参数
      SensorAlartData: {}
    };
  },
  created() {
    this.getList();
    // 接收路由跳转参数
    this.getRouterPara();
  },
  beforeDestroy() {
    if (intervalKey) {
      clearInterval(intervalKey);
    }
  },
  methods: {
    getRouterPara() {
      this.SensorAlartData = this.$route.query.SensorAlartData || {};
      this.queryParams.sorId = this.SensorAlartData.sorId ? String(this.SensorAlartData.sorId) : '';
      intervalKey = setTimeout(() => {
        this.handleQuery();
      }, 1000);
    },

    //搜索时间选择
    dateHandler() {
      this.queryParams.startTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
    },

    /** 查询数据列表 */
    getList() {
      this.$modal.loading("正在努力请求数据，请稍候...");
      faultList({
        ...this.queryParams,
        sorId:
          Number(this.queryParams.sorId) == 0
            ? undefined
            : Number(this.queryParams.sorId)
      }).then(response => {
        this.allList = response.data.rows.map(item => {
          const ojb = { ...item };
          ojb.qaType = "无";
          if (item.longitude && item.latitude) {
            ojb.lonAndLat =
              item.longitude.toFixed(3) + "," + item.latitude.toFixed(3);
          }
          if (item.ifAlarm == 2) {
            ojb.qaType = "报警";
          }
          if (item.ifFault == 2) {
            ojb.qaType += ", 故障";
          }

          return ojb;
        });
        this.total = response.data.total;
        this.infoList = this.allList.slice(
          (this.queryParams.pageNum - 1) * this.queryParams.pageSize,
          this.queryParams.pageNum * this.queryParams.pageSize
        );
        this.$modal.closeLoading();
      });
    },
    // 分页
    getPageData() {
      this.infoList = this.allList.slice(
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize,
        this.queryParams.pageNum * this.queryParams.pageSize
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        sorId: "",
        address: "",
        startTime: formatDate(
          new Date().setFullYear(new Date().getFullYear() - 1)
        ),
        endTime: formatDate(new Date())
      };
      this.dateRange = [
        formatDate(new Date().setFullYear(new Date().getFullYear() - 1)),
        formatDate(new Date())
      ];
      this.handleQuery();
    },
    switchQuery() {
      this.isShowQuery = !this.isShowQuery;
      this.queryParams.sorId = "";
    },
    /** 导出按钮操作 */
    handleExport(row) {
      this.download(
        "data/sensorpoints/exportFault",
        {
          ...this.queryParams
        },
        `故障数据_${new Date().getTime()}.xlsx`
      );
    },
    handleDetail(row) {
      this.title = ` ${row.sorId} 历史数据`;
      this.curSenorID = row.sorId;
      this.open = true;
    },
    //echart曲线时间选择
    dateDialogHandler(par) {
      if (par === null) {
        this.getTempAndVoltage();
        this.dateDialogRange = [];
      } else {
        this.getTempAndVoltage(par[0], par[1]);
      }
    },
    //echart曲线时间接口
    getTempAndVoltage(startTime = "", endTime = "") {
      // if (startTime != "" && endTime != "")
      this.$modal.loading("正在努力请求数据，请稍候...");

      getTempAndSensorvoltage({
        startTime,
        endTime,
        sorId: Number(this.curSenorID)
      }).then(response => {
        const { sensortempList, sensorvoltageList, timeList } = response.data;

        let base = +new Date(1968, 9, 3);
        let oneDay = 24 * 3600 * 1000;
        let date = [];
        let data = [Math.random() * 300];
        let data1 = [Math.random() * 500];
        for (let i = 1; i < 20000; i++) {
          var now = new Date((base += oneDay));
          date.push(
            [now.getFullYear(), now.getMonth() + 1, now.getDate()].join("/")
          );
          data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1]));
          data1.push(Math.round((Math.random() - 0.5) * 80 + data1[i - 1]));
        }
        let isUpdate = false;
        if (
          (this.sensortempList != "" &&
            this.sensortempList != sensortempList) ||
          (this.sensorvoltageList != "" &&
            this.sensorvoltageList != sensorvoltageList)
        ) {
          isUpdate = true;
        }
        this.sensortempList = sensortempList;
        this.sensorvoltageList = sensorvoltageList;
        const option = {
          tooltip: {
            trigger: "axis",
            axisPointer: {
              type: "cross",
              label: {
                backgroundColor: "#6a7985"
              }
            },
            position: function (pt) {
              return [pt[0], "10%"];
            }
          },

          title: {
            left: "center",
            text: "传感器历史温度、电压"
          },
          xAxis: {
            type: "category",
            boundaryGap: false,
            data: timeList
          },
          yAxis: {
            type: "value",
            boundaryGap: false
          },

          series: [
            {
              name: "温度",
              type: "line",
              symbol: "none",
              sampling: "lttb",
              itemStyle: {
                color: "rgb(255, 70, 131)"
              },
              areaStyle: {
                color: this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "rgb(255, 158, 68)"
                  },
                  {
                    offset: 1,
                    color: "rgb(255, 70, 131)"
                  }
                ])
              },
              data: this.sensortempList
            },
            {
              name: "电压",
              type: "line",
              symbol: "none",
              sampling: "lttb",
              itemStyle: {
                color: "#007cff"
              },
              areaStyle: {
                color: this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#007cff"
                  },
                  {
                    offset: 1,
                    color: "rgba(61, 42, 208,0)"
                  }
                ])
              },
              data: this.sensorvoltageList
            }
          ]
        };

        if (this.myChart) this.myChart.setOption(option, isUpdate);
        // this.open = true;

        this.$modal.closeLoading();
      });
    },
    closeDialog() {
      this.open = false;
      this.dateDialogRange = [];
      if (this.myChart) {
        this.myChart.clear();
        this.myChart = null;
      }
    },
    initChart() {
      this.myChart = this.$echarts.init(document.getElementById("myChart"));
      this.getTempAndVoltage();
    },
  }
};
</script>
<style lang="scss" scoped>
#myChart {
  width: 960px;
  height: 420px;
}

.query {
  border-top: 1px solid #dfe6ec;
  border-bottom: 1px solid #dfe6ec;
  text-align: right;
}

.query>>>.el-form--inline .el-form-item {
  margin-right: 0;
  margin-left: 10px;
  margin-bottom: 14px;
  margin-top: 14px;
}

.app-container>>>.el-button--text:hover {
  border-color: #0000;
  background: #0000;
}

::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: 1px solid #4b5057 !important;
}

::v-deep .el-table::before,
.el-table--group::after,
.el-table--border::after {
  border: solid 1px #4b5057 !important;
}

::v-deep .el-table--border:after,
.el-table--group:after,
.el-table:before {
  background-color: #4b5057 !important;
}

::v-deep .el-table--border,
.el-table--group {
  border-color: #4b5057 !important;
}

::v-deep.el-table td,
.el-table th.is-leaf {
  border-bottom: 1px solid #4b5057 !important;
}

::v-deep .el-table--border th,
.el-table--border th.gutter:last-of-type {
  border-bottom: 1px solid #4b5057 !important;
}

::v-deep.el-table--border td,
.el-table--border th {
  border-right: 1px solid #4b5057 !important;
}

::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: 1px solid rgba(255, 255, 255, 0.17);
  border-right: 1px solid #4b5057 !important;
}

::v-deep .el-table tbody tr:hover>td {
  background-color: #1d2125 !important;
  color: #ffffff;
}

.checkDetail {
  font-family: "AlibabaPuHuiTi-2-55-Regular";
  font-size: 15px;
  font-weight: normal;
  font-stretch: normal;
  letter-spacing: 1px;
  color: #005aff;
  cursor: pointer;
}
</style>

<style lang="scss">
// .lcj-select-popup {
// background: #08102a !important;
// border: none !important;
// color: #fff !important;
// .el-picker-panel__footer {
//   border: 1px solid rgba(255, 255, 255, 0.17) !important;
//   background-color: #08102a;
// }
// .el-date-range-picker__time-header,
// .el-date-table th {
//   border-bottom: 1px solid rgba(255, 255, 255, 0.17);
// }
// .el-date-range-picker__content.is-left {
//   border-bottom: 0px solid rgba(255, 255, 255, 0.17);
// }
// .el-time-panel__content.has-seconds::before,
// .el-time-panel__content.has-seconds::after {
//   border-bottom: 1px solid #5c5e63;
//   border-top: 1px solid #5c5e63;
// }
// .el-time-panel__btn {
//   border: none;
//   line-height: 28px;
//   padding: 0 5px;
//   margin: 0 5px;
//   cursor: pointer;
//   font-family: "AlibabaPuHuiTi-2-105-Heavy";
//   font-size: 14px;
//   color: #009688;
// }

// .el-time-panel__btn.confirm,
// .el-time-spinner__item.active:not(.disabled) {
//   font-weight: 800;
//   color: #009688;
// }
// .el-time-spinner__item:hover:not(.disabled):not(.active) {
//   background: transparent;
//   color: #009688;
//   cursor: pointer;
// }
// .el-date-range-picker__content.is-left {
//   border-right: 1px solid rgba(255, 255, 255, 0.17);
// }
// .el-time-panel,
// .el-time-panel__footer {
//   border-top: 1px solid rgba(255, 255, 255, 0.17);
//   background-color: #08102a;
//   border: 1px solid rgba(255, 255, 255, 0.17);
// }
// .popper__arrow {
//   border-bottom-color: #439eff !important;
//   &::after {
//     border-bottom-color: #08102a !important;
//   }
// }
// .el-input__inner,
// .el-input.is-disabled .el-input__inner {
//   height: 36px;
//   line-height: 36px;
//   background: #1b3663;
//   color: #fff;
//   font-family: "AlibabaPuHuiTi-2-55-Regular";

//   border: none;
// }
// .el-button.is-disabled.is-plain,
// .el-button.is-disabled.is-plain:hover,
// .el-button.is-disabled.is-plain:focus {
//   background-color: transparent !important;
//   border: none !important;
//   color: #c0c4cc !important;
// }
// .el-button + .el-button,
// .el-button--text {
//   background: transparent;
//   border: none;
//   span {
//     font-family: "AlibabaPuHuiTi-2-105-Heavy";
//     font-size: 16px;
//     color: #25b390;
//   }
// }
// .el-select-dropdown__item {
//   color: #fff;
//   &:hover {
//     background: rgba(10, 152, 188, 0.3);
//     color: #202d38;
//   }
// }
// .el-select-dropdown__item.hover {
//   background: rgba(10, 152, 188, 0.3);
//   color: #202d38;
// }
// .el-scrollbar__wrap {
//   overflow-x: hidden;
//   margin-bottom: 0 !important;
// }
// .el-cascader-menu {
//   border-right: solid 1px #439eff;
// }
// // .el-radio {
// //   display: none;
// // }
// .el-cascader-node.in-active-path,
// .el-cascader-node.is-selectable.in-checked-path,
// .el-cascader-node.is-active {
//   color: rgba(10, 152, 188, 0.3);
//   font-weight: bold;
// }
// .el-date-table td.in-range div,
// .el-date-table td.in-range div:hover,
// .el-date-table.is-week-mode .el-date-table__row.current div,
// .el-date-table.is-week-mode .el-date-table__row:hover div {
//   background-color: transparent;
// }
// .el-date-table td.end-date span,
// .el-date-table td.start-date span {
//   background-color: #08102a;
// }
// .el-cascader-node__label {
//   color: #fff;
//   z-index: 1;
//   pointer-events: none;
// }
// .el-cascader-node:not(.is-disabled):hover,
// .el-cascader-node:not(.is-disabled):focus {
//   background: #08102a;
// }
// .el-cascader-node__postfix {
//   right: 30px;
//   color: #08102a;
// }
// .el-radio__inner {
//   border: 0;
//   border-radius: 0;
//   width: 170px;
//   height: 34px;
//   background-color: transparent;
//   position: absolute;
//   top: -18px;
//   left: -19px;
// }

// .el-select-dropdown__item {
//   color: #fff;
//   &:hover {
//     background: #08102a;
//     color: #fff;
//   }
//   &:focus {
//     background: #08102a;
//     color: #fff;
//   }
// }
// .el-select-dropdown__item.selected {
//   color: #fff;
//   background: #08102a;
// }
// }
// .el-date-editor {
//   .el-range-input {
//     background: transparent;
//     font-family: "AlibabaPuHuiTi-2-55-Regular";
//     font-size: 14px;
//     font-weight: normal;
//     font-stretch: normal;
//     line-height: 18px;
//     letter-spacing: 0px;
//     color: #cccccc;
//   }
//   .el-range__icon {
//     color: #fff;
//   }
//   .el-range-separator {
//     line-height: 27px !important;
//     width: 7% !important;
//     font-family: "AlibabaPuHuiTi-2-105-Heavy";
//     font-size: 16px;
//     font-weight: normal;
//     font-stretch: normal;
//     line-height: 18px;
//     letter-spacing: 0px;
//     color: #ffffff;
//   }
// }
// .el-date-table td.today span {
//   color: #25b390;
//   font-weight: 700;
//   font-size: 16px;
//   font-family: "AlibabaPuHuiTi-2-105-Heavy";
// }
// .el-date-table td.in-range div,
// .el-date-table td.in-range div:hover {
//   background-color: rgba(37, 179, 144, 0.3) !important;
// }
// .el-date-table td.in-range div,
// .el-date-table td.in-range div:hover,
// .el-date-table.is-week-mode .el-date-table__row.current div,
// .el-date-table.is-week-mode .el-date-table__row:hover div {
//   background-color: transparent;
// }
// .el-date-table td.end-date span,
// .el-date-table td.start-date span {
//   background-color: #25b390;
// }
// .el-popper[x-placement^="top"] .popper__arrow::after,
// .el-popper[x-placement^="top"] .popper__arrow {
//   border-top-color: transparent;
// }
//
</style>
